package cn.zglbk.data.csv;

import java.io.Serializable;
import java.util.*;

/**
 * @author 猪哥亮
 *
 */
public class StaticConfigMessage {
    private static class Single{
        public static final StaticConfigMessage INSTANCE = new StaticConfigMessage();
    }
    private Map<Class<?>, FileHandler> allData = new HashMap<>();
    public static StaticConfigMessage getInstance(){
        return  Single.INSTANCE;
    }

    /**
     * 添加导表数据
     * @param clazz
     * @param handler
     */
    public <T extends FileTable> void put(Class<?> clazz, FileHandler<T> handler){
        allData.put(clazz,handler);
    }

    /**
     *获取导表数据对应的类
     * @param clazz
     * @param id
     * @param <T>
     * @return
     */
    public <T extends FileTable> T get(Class<T> clazz, Serializable id){
        FileHandler<T> map = allData.get(clazz);
        if(map == null) {
            return null;
        }
        return (T) map.getById(id);
    }
    public <T extends FileTable> Map<Serializable,T> getMap(Class<?> clazz){
        if(!allData.containsKey(clazz)) {
            return new HashMap<>(0);
        }

        return allData.get(clazz).getMap();
    }
    public <T extends FileTable> List<T> getList(Class<T> clazz){
        FileHandler<T> map = allData.get(clazz);
        if(map == null) {
            return new ArrayList<>();
        }
        return map.getArr();
    }
    public <T extends FileTable> boolean init(String path,Class<T> clazz){
        FileHandler<T> handler = new FileHandler<>();
        boolean init = handler.init(path, clazz);
        if(init){
            this.allData.put(clazz,handler);
        }
        return init;
    }
}